package com.mapmymotion;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;

import android.os.Environment;


public class ChatClient  
{
	   public static Socket socket = null;
	   public static BufferedReader in = null;
	   public static PrintWriter out = null;	
	   public static StringBuffer toSend = new StringBuffer("");
	   public static String hostIP = "96.31.86.130";
	   //public static String hostIP = "206.51.233.6";
	   public static int port = 2002;
	   //public static int port = 8787;
	   
public String setClient() {	
		try{
			/*Put the current IP address for current machine  
			if you didn't have an actual server and clients
			if you have an actual server and clients put the client IP address*/
			socket = new Socket(hostIP,port);
			socket.setKeepAlive(true);
			
		    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		    out = new PrintWriter(socket.getOutputStream(), true);
	

			Thread th;
			th = new Thread();
			th.start();
			
		return "CONNECTED";	
		}catch(Exception e){return e.getMessage();}
		
		
	}

	public void sendData( String str)
	{ 
		if (! socket.isConnected()|| socket.isClosed()){
			setClient();
		}
		toSend.append(str);
        try {
            // Send data
            if (toSend.length() != 0) {
               out.print(toSend); out.flush();
               toSend.setLength(0);
            }
            }
            catch(Exception m){}
		 }
				  
//Send a complete array of data
	public void sendData(StringBuffer toSend){
		if (! socket.isConnected()|| socket.isClosed()){
			setClient();
		}

        try {        	
            // Send data
            if (toSend.length() != 0) {
               out.print(toSend); out.flush();
               //REMOVED 6/10/2011 writeToFile(toSend );
               toSend.setLength(0);
            }
            }
            catch(Exception m){}
	};	
	
	

// Cleanup for disconnect
public String cleanUp() {

   try {
      if (socket != null) {
         socket.close();
         socket = null;
         return "DISCONNECTED";
      }
   }
   catch (IOException e) { socket = null;return e.getMessage(); }

   try {
      if (in != null) {
         in.close();
         in = null;
         return "Stream Closed";
      }
   }
   catch (IOException e) { in = null;return e.getMessage(); }

   if (out != null) {
      out.close();
      out = null;
   }
return "App Closed";

}

private String result;
private String saveFileName = "activity_savefile";

private String writeToFile( StringBuffer sbuffer ){
	  FileOutputStream fos = null;
	  ObjectOutputStream out = null;

	  try{
	   File root = Environment.getExternalStorageDirectory();

	   if( root.canWrite() ){
	    fos = new FileOutputStream( root + "/" + saveFileName );
	    out = new ObjectOutputStream( fos );
	    out.writeObject(sbuffer );

	    result = "File written";

	    out.close();
	   }else{
	    result = "file cant write";
	   }
	  }catch( IOException e ){
	   e.printStackTrace();
	   result = "file not written";
	  }

	  return result;
	 }



}